package com.example.chinesenation.Utils;

import com.alibaba.fastjson.JSON;
import com.example.chinesenation.Entity.Article;
import com.example.chinesenation.Entity.Policy;
import com.example.chinesenation.VO.*;
import com.example.chinesenation.VO.MainPage.VOArticleList;
import com.example.chinesenation.VO.MainPage.VOPolicyList;
import com.example.chinesenation.VO.NationPage.VONationArticle;
import com.example.chinesenation.VO.Record.VORecord;
import com.example.chinesenation.VO.Search.VOArticleSearch;
import com.example.chinesenation.VO.Search.VOPolicySearch;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;

/**
 * @author sakuraFallingDown
 * @version 1.0
 * @date 2021/1/22 18:28
 */
public class DataChanges {//类的数据转换工具类

    private final static String divCenterStart="<div class='text' style='text-align:center; font-size:15px'>";
    private final static String divCenterEnd="</div>";


    public static List<String> fromJsonToListString(String json){
        Content content = JSON.parseObject(json,Content.class);
        return content.getContent();
    }

    public static String findTheFirstURL(List<String> content){
        for(String str:content){
            if(str.contains(".png")||str.contains(".jpg")) return str;
        }
        return null;
    }
    public static int getUrlType(String url){
        int type=0;
        if(url.contains(".png")||url.contains(".jpg")) type=1;
        if(url.contains(".mp3")||url.contains(".mp4")) type=2;
        return type;

    }
    public static void filterAndAddURLPath(List<String> content){//清除文章的不合法内容，并且给url加前缀路径
        boolean flag=false;
        int imgSize=1;//
        for(int i=0;i<content.size();i++){
            String str=content.get(i);
            if(flag){
                if(!str.contains("图")&&!str.equals("nan")&&!str.equals("")){
                    str="图"+imgSize+":"+str;
                    imgSize++;
                }
                content.remove(i);
                content.add(i,divCenterStart+str+divCenterEnd);
                flag=false;
            }
            if(str.contains(".png")||str.contains(".jpg")||str.contains(".bmp")){
                content.remove(i);
                content.add(i,CommonURL.imgUrl+str);
                flag=true;
            } else if (str.equals("nan")) {
                content.remove(i);
                content.add(i,"");
            } else if(str.contains(".mp4")){
                content.remove(i);
                content.add(i,CommonURL.imgUrl+str);
            }
        }
    }
    //寻找以content（JSON格式）的String字符串中的第一个可以代表内容的段落内容，并且返回字段裁剪
    public static String findTheFirstUsefulParagraph(String content){
        List<String> contentList = fromJsonToListString(content);
        String first= "";
        for (String string : contentList) {
            if(!(string.contains("摘要")
                    ||string.contains("摘 要")
                    ||string.contains("nan")
                    ||string.contains(".jpg")
                    ||string.contains(".png")
                    ||string.contains(".bmp")
                    ||string.contains("提要")
                    ||string.contains("【")
                    ||string.contains("["))){
                first=string;
                break;
            }
        }
        return TextTools.handlerLongString(first);//处理过长数据
    }


    public static void fromArticleToVOArticleList(Article article, VOArticleList voArticleList){

        voArticleList.setId(article.getId());
        voArticleList.setTitle(article.getTitle());
        voArticleList.setPublishTime(article.getPublishDate());
        String img=findTheFirstURL(fromJsonToListString(article.getContent()));
        if(img!=null){
            voArticleList.setUrl(CommonURL.imgUrl +img);
        }
        voArticleList.setPageview(article.getPageview());
        voArticleList.setCategory(article.getCategoryName());
        voArticleList.setModule(article.getModuleName());
        voArticleList.setUrlType(0);
    }

    public static void fromPolicyToVOPolicyList(Policy policy, VOPolicyList voPolicyList){

        voPolicyList.setId(policy.getId());
        voPolicyList.setTitle(policy.getTitle());
        voPolicyList.setPublishTime(policy.getPublishTime());
        voPolicyList.setPublisher(policy.getPublisher());
        String img=findTheFirstURL(fromJsonToListString(policy.getContent()));
        if(img!=null){
            voPolicyList.setUrl(CommonURL.imgUrl +img);
        }
        voPolicyList.setPageview(policy.getPageview());
        voPolicyList.setType(getPolicyType(policy));
    }
    public static int getPolicyType(Policy policy){//判断policy是新 还是热门

        int type=0;//0为默认，1为新，2热门
        int pageview=policy.getPageview();
        Date date=policy.getPublishTime();
        Date yesterday=new Date(System.currentTimeMillis()-3*864000000);//三十天之内的时间
        if(pageview>50){
            if(date.after(yesterday)){
                type=2;
            }else{
                type=new Random().nextInt(3);
            }
        }
        return type;
    }


    public static void fromArticleToVOArticle(Article article,VOArticle voArticle){
        voArticle.setCategoryName(article.getCategoryName());
        voArticle.setId(article.getId());
        voArticle.setModuleName(article.getModuleName());
        voArticle.setNation(article.getNationName());
        voArticle.setPageview(article.getPageview());
        voArticle.setPublishDate(article.getPublishDate());
        voArticle.setTag(article.getTag());
        voArticle.setTitle(article.getTitle());
        voArticle.setContent(fromJsonToListString(article.getContent()));
        filterAndAddURLPath(voArticle.getContent());//过滤以及添加图片映射路径
    }

    public static void fromPolicyToVOPolicy(Policy policy, VOPolicy voPolicy){
        voPolicy.setId(policy.getId());
        voPolicy.setNation(policy.getNationName());
        voPolicy.setPageview(policy.getPageview());
        voPolicy.setPublishDate(policy.getPublishTime());
        voPolicy.setTag(policy.getPublisher());
        voPolicy.setTitle(policy.getTitle());
        voPolicy.setContent(fromJsonToListString(policy.getContent()));
        filterAndAddURLPath(voPolicy.getContent());//过滤以及添加图片映射路径
    }

    public static void fromPolicyToVORecord(Policy policy, VORecord voRecord){
        voRecord.setId(policy.getId());
        voRecord.setTitle(policy.getTitle());
        voRecord.setPublisher(policy.getPublisher());
        String img=findTheFirstURL(fromJsonToListString(policy.getContent()));
        if(img!=null){
            voRecord.setUrl(CommonURL.imgUrl +img);
            voRecord.setType(1);//默认为图片 视频为2
        }

    }

    public static void fromArticleToVORecord(Article article,VORecord voRecord){
        voRecord.setId(article.getId());
        voRecord.setTitle(article.getTitle());
        voRecord.setPublisher(article.getTag());
        String img=findTheFirstURL(fromJsonToListString(article.getContent()));
        if(img!=null){
            voRecord.setUrl(CommonURL.imgUrl +img);
            voRecord.setType(1);//默认为图片 视频为2
        }
    }

    public static void fromPolicyToVOPolicySearch(Policy policy, VOPolicySearch voPolicySearch) {
        voPolicySearch.setId(policy.getId());
        voPolicySearch.setTitle(policy.getTitle());
        voPolicySearch.setPublisher(policy.getPublisher());
        voPolicySearch.setPublishTime(policy.getPublishTime());
        voPolicySearch.setPageview(policy.getPageview());
        String img=findTheFirstURL(fromJsonToListString(policy.getContent()));
        if(img!=null){
            voPolicySearch.setUrl(CommonURL.imgUrl +img);
            voPolicySearch.setType(1);//默认为图片 视频为2
        }
        //传第一段可用的段落填充其内容，以下调用函数想用
        voPolicySearch.setContent(findTheFirstUsefulParagraph(policy.getContent()));
    }
    public static void fromArticleToVOArticleSearch(Article article, VOArticleSearch voArticleSearch) {
        voArticleSearch.setId(article.getId());
        voArticleSearch.setTitle(article.getTitle());
        voArticleSearch.setPublisher(article.getTag());
        voArticleSearch.setPublishTime(article.getPublishDate());
        voArticleSearch.setPageview(article.getPageview());
        voArticleSearch.setCategoryName(article.getCategoryName());
        String img=findTheFirstURL(fromJsonToListString(article.getContent()));
        if(img!=null){
            voArticleSearch.setUrl(CommonURL.imgUrl +img);

            voArticleSearch.setType(1);//默认为图片 视频为2
        }

        voArticleSearch.setContent(findTheFirstUsefulParagraph(article.getContent()));

    }
    public static void fromArticleToVONationArticle(Article article, VONationArticle voNationArticle) {
        voNationArticle.setId(article.getId());
        voNationArticle.setTitle(article.getTitle());
        voNationArticle.setPublishTime(article.getPublishDate());
        String img=findTheFirstURL(fromJsonToListString(article.getContent()));
        if(img!=null){
            voNationArticle.setUrl(CommonURL.imgUrl +img);
        }
        voNationArticle.setUrlType(0);
        voNationArticle.setContent(findTheFirstUsefulParagraph(article.getContent()));

    }



}
